package NiuKe.JianZhiOffer;
import  java.util.*;
public class JZ4 {
    public static void main(String[] args) {
        JZ4 jz = new JZ4();
        int a[] = {1,2,4,7,3,5,6,8};
        int b[] = {4,7,2,1,5,3,8,6};
        /*int index = 0;
        for (; index < b.length; index++){
            if(b[index] == a[0] ){
                break;
            }
        }
       // System.out.println(index);*/
        TreeNode head = jz.reConstructBinaryTree(a,b);
/*        int c[] = Arrays.copyOfRange(a,1,index + 1);

        for(int i : c){
            System.out.println(i);
        }*/

    }
    public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
        if(pre.length == 0) return null;
        if(pre.length == 1) return new TreeNode(pre[0]);
        int index = 0;
        for (; index < vin.length; index++){
            if(vin[index] == pre[0] ){
                break;
            }
        }
        TreeNode head  = new TreeNode(pre[0]);
        head.left = reConstructBinaryTree(
                Arrays.copyOfRange(pre,1,index + 1),
                Arrays.copyOfRange(vin,0,index)
        );

        head.right = reConstructBinaryTree(
                Arrays.copyOfRange(pre,index + 1,pre.length),
                Arrays.copyOfRange(vin,index + 1,vin.length)
        );
        return head;
    }
}
